package com.lry.sso.auth.server.controller;

import com.lry.sso.auth.common.util.SessionInfo;
import com.lry.sso.auth.server.service.JwtTokenService;
import com.lry.sso.common.entity.response.ResultData;
import com.lry.sso.common.entity.response.Ret;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * token 服务接口
 *
 * @author 李瑞益
 * @date 2020-02-12 20:29
 */
@RestController
@RequestMapping("/sso/auth/v1/token")
public class JwtTokenController {

    @Autowired
    private JwtTokenService jwtTokenService;

    /**
     * 用户登录公钥
     * @return 公钥
     */
    @GetMapping("/public-key")
    public ResultData<byte[]> getPublicKey() {
        byte[] result = jwtTokenService.getUserPublicKey();
        return Ret.data(result);
    }

    /**
     * 解析密码
     * @param password 密码
     * @return 明文密码
     */
    @PostMapping("/rsa-parse")
    public String parsePasswrod(String password){
        String result = null;
        try {
            result = jwtTokenService.parsePassword(password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 获取token
     * @param sessionInfo 用户信息
     * @return token
     */
    @PostMapping("")
    public String makeToken(@RequestBody SessionInfo sessionInfo) {
        String token = null;
        try {
            token = jwtTokenService.getToken(sessionInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }

    /**
     * 解析token
     * @param token token
     * @return null：解析失败即验证失败
     */
    @PostMapping("/verify")
    public SessionInfo verifyToken(String token) {
        SessionInfo result = null;
        try {
            result = jwtTokenService.parseToken(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取过期实际
     * @param token token
     * @return 过期实际
     */
    @GetMapping("/expire")
    public Long getExpire(String token) {
        Long result = 0L;
        try {
            result = jwtTokenService.getExpire(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
